package com.spit.fammo.bean.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.spit.fammo.bean.db.AssetBean
import kotlinx.coroutines.flow.Flow

/**
 * @Author nike
 * @Date 2023/7/28 10:34
 * @Description
 */
@Dao
interface AssetDao {

    /**
     * 增加
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun add(vararg bean: AssetBean)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun addAll(bean: List<AssetBean>)
    /**
     * 删除
     */
    @Delete
    suspend fun delete(bean: AssetBean)

    @Query("DELETE FROM AssetBean WHERE userId = :userId")
    suspend fun deleteUserAll(userId: String?)

    //删除数据库表全部数据
    @Query("delete from AssetBean")
    suspend fun deleteAll()

    /**
     * 查询所有
     */
    @Query("SELECT * FROM AssetBean WHERE userId = :userId")
    fun findAll(userId: String?): Flow<List<AssetBean>>

    @Query("SELECT * FROM AssetBean WHERE assetNo = :assetNo AND stocktakeno = :stocktakeno")
    fun findAssetById(assetNo: String?, stocktakeno: String?): AssetBean

    @Query("SELECT * FROM AssetBean WHERE epc COLLATE NOCASE =:epc AND stocktakeno = :stocktakeno AND userId = :userId")
    fun findEpcId(epc: String?, stocktakeno: String?, userId: String?): AssetBean
    @Query("SELECT * FROM AssetBean WHERE epc COLLATE NOCASE IN (:epcList) AND stocktakeno = :stocktakeno AND userId = :userId AND type = :type AND epc IS NOT NULL AND epc <> ''")
    fun findEpcIdList(epcList: List<String?>, stocktakeno: String?, userId: String?, type: Int): List<AssetBean>
    @Query("SELECT * FROM AssetBean WHERE epc COLLATE NOCASE IN (:epcList) AND stocktakeno = :stocktakeno AND userId = :userId AND epc IS NOT NULL AND epc <> ''")
    fun findEpcIdList(epcList: List<String?>, stocktakeno: String?, userId: String?): List<AssetBean>
    @Query("SELECT * FROM AssetBean WHERE assetCode COLLATE NOCASE =:assetCode AND stocktakeno = :stocktakeno AND userId = :userId")
    fun findAssetCodeId(assetCode: String?, stocktakeno: String?, userId: String?): AssetBean

    @Query("SELECT * FROM AssetBean WHERE userId = :userId AND stocktakeno = :orderId AND type =:type")
    fun findAll(orderId: String?, type: Int, userId: String?): List<AssetBean>

    // 合并查询逻辑
    @Query("SELECT * FROM AssetBean WHERE (epc COLLATE NOCASE = :epc OR assetCode COLLATE NOCASE = :assetCode) AND stocktakeno = :stocktakeno AND userId = :userId")
    fun findAsset(epc: String?, assetCode: String?, stocktakeno: String?, userId: String?): AssetBean?

    /**
     *  保存上传
     */
    @Query("SELECT * FROM AssetBean WHERE stocktakeno = :stocktakeno AND userId = :userId AND (type = 1 OR type = 2)")
    fun findById(stocktakeno: String?, userId: String?): List<AssetBean>

    @Query("SELECT * FROM AssetBean WHERE userId = :userId AND stocktakeno = :orderId AND type != 2")
    fun findAll(orderId: String?, userId: String?): List<AssetBean>

    /**
     * 更新
     */
    @Update
    suspend fun update(bean: AssetBean)

    @Update
    suspend fun updateList(bean: List<AssetBean>)

}